// 题目链接：https://www.luogu.com.cn/record/208761113

#include <iostream>
#include <algorithm>

using namespace std;

const int N = 50010;
int n, k;
int a[N];
int cnt_lr[N], cnt_rl[N];

int main()
{
    int i, j;
    int res = 0;
    scanf("%d %d", &n, &k);
    for (int i = 1; i <= n; ++i)
        scanf("%d", &a[i]);

    sort(a + 1, a + n + 1);
    cnt_lr[0] = 0;
    cnt_rl[n + 1] = 0;

    i = 1, j = 1;
    while (j <= n)
    {
        while (j <= n && a[j] - a[i] <= k)
        {
            cnt_lr[j] = max(cnt_lr[j - 1], j - i + 1);
            ++j;
        }
        ++i;
    }

    i = n, j = n;
    while (j > 0)
    {
        while (j > 0 && a[i] - a[j] <= k)
        {
            cnt_rl[j] = max(cnt_rl[j + 1], i - j + 1);
            --j;
        }
        --i;
    }

    for (int k = 1; k < n; ++k)
    {
        res = max(res, cnt_lr[k] + cnt_rl[k + 1]);
    }
    printf("%d", res);

    return 0;
}